﻿/**********************************************************************************
 * Project Name :   
 * Remarks      :  
 * Class Name   :  UICommon 
 * Version      :  v1.0.0.0 
 * Author       :  Joey Liang 
 * Description  : 
 * Created Time :  2013/8/14 22:38:59
 * Update Man   :  Joey Liang
 * Update Time  :  2013/8/14 22:38:59
************************************************************************/

using JoeyLibs.IdleStartMenu.Model;
using JoeyLibs.Utility;
using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Media;

namespace JoeyLibs.IdleStartMenu.UIUtility
{
    public class UICommon
    {
        public static string RandomFileName
        {
            get
            {
                return DateTime.Now.ToString(LanguageResource.Code_BuildBackupFile);
            }
        }

        public static string CurrentDirectory
        {
            get { return AppDomain.CurrentDomain.BaseDirectory; }
        }

        public static string GetAppFile(string fileName)
        {
            return CurrentDirectory + fileName;
        }

        public static OpenFileDialog OpenBackupFileDialog(string fileName = "")
        {
            OpenFileDialog ofd = new OpenFileDialog();
            string suffix = AppConfig.Instance["BackupSuffix"];
            string directory = 
                    AppDomain.CurrentDomain.BaseDirectory.InsertLast(AppConfig.Instance["BackupPath"]);
            if (string.IsNullOrEmpty(fileName))
            {
                ofd.InitialDirectory = directory;
            }
            else
            {
                fileName = directory.InsertLast(fileName).InsertLast(suffix);
                ofd.FileName = fileName;
                //CreateFile(fileName);
            }
                        
            ofd.DefaultExt = "*".InsertLast(suffix);
            ofd.Filter = string.Format("{0}(*{1})|*{1}", LanguageResource.Title_BackupFileDialogTitle, suffix);
            return ofd;
        }

        public static void CreateFile(string fileName)
        {
            File.Create(fileName);
        }

        public static Process StartProcess(string fileName, string arguments)
        {
            try
            {
                return Process.Start(fileName, arguments); 
            }
            catch (Win32Exception)
            {
                MsgBox.Show(LanguageResource.Exception_Win32ProcessError);
            }
            return null;
        }

        public static void SetButtonClickStyle(Button btn, bool set = true)
        {
            if (btn == null)
                return;

            Color blackColor = set ? Color.FromRgb(255, 0, 0) : Color.FromRgb(255, 255, 255);
            btn.BorderBrush = new SolidColorBrush(blackColor);
        }

        public static void ClearTextBox(params TextBox[] txts)
        {
            if (txts != null || txts.Length > 0)
            {
                txts.Foreach<TextBox>(p => p.Clear());
            }
        }

        public static Button CreateWP8Button(double height, double width)
        {
            Button btn = new Button();
            btn.Height = height;
            btn.Width = width;

            Color blackColor = Color.FromRgb(255, 255, 255);
            btn.Foreground = new SolidColorBrush(blackColor);
            btn.BorderBrush = new SolidColorBrush(blackColor);
            btn.Background = new SolidColorBrush(Color.FromRgb(0, 0, 0));

            return btn;
        }
    }
    
    public enum FormHandleState
    {
        Default = 0,
        Add,
        Edit
    }
}
